package com.lemon.smartsafetynew.util.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;

/**
 * Created by Andrew on 2017/7/17.
 */

public class AppSharePreferenceUtil {

    private static SharedPreferences sharedPreferences = null;
    private static SharedPreferences.Editor editor = null;

    private static final AppSharePreferenceUtil ourInstance = new AppSharePreferenceUtil();

    public static AppSharePreferenceUtil getInstance() {
        return ourInstance;
    }

    private AppSharePreferenceUtil() {
        init();
    }

    private void init() {
        sharedPreferences = IApplication.context().getSharedPreferences(SafeCloudConstants.SAFE_CLOUD_V2, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    /**
     * 向SP存入指定key对应的数据
     * 其中value可以是String、boolean、float、int、long等各种基本类型的值
     * @param key
     * @param value
     */
    public void putString(String key, String value) {

        if(sharedPreferences == null) {
            init();
        }

        editor.putString(key, value);
        editor.apply();
    }

    public void putBoolean(String key, boolean value) {

        if(sharedPreferences == null) {
            init();
        }

        editor.putBoolean(key, value);
        editor.apply();
    }

    public void putFloat(String key, float value) {

        if(sharedPreferences == null) {
            init();
        }

        editor.putFloat(key, value);
        editor.apply();
    }

    public void putInt(String key, int value) {

        if(sharedPreferences == null) {
            init();
        }

        editor.putInt(key, value);
        editor.apply();
    }

    public void putLong(String key, long value) {

        if(sharedPreferences == null) {
            init();
        }

        editor.putLong(key, value);
        editor.apply();
    }



    /**
     * 获取SP数据里指定key对应的value。如果key不存在，则返回默认值defValue。
     * @param key
     * @param defValue
     * @return
     */
    public String getString(String key, String defValue) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.getString(key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.getBoolean(key, defValue);
    }

    public float getFloat(String key, float defValue) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.getFloat(key, defValue);
    }

    public int getInt(String key, int defValue) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.getInt(key, defValue);
    }

    public long getLong(String key, long defValue) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.getLong(key, defValue);
    }

    /**
     * 清空SP里所以数据
     */
    public void clear() {

        if(sharedPreferences == null) {
            init();
        }

        editor.clear();
        editor.apply();
    }

    /**
     * 删除SP里指定key对应的数据项
     * @param key
     */
    public void remove(String key) {

        if(sharedPreferences == null) {
            init();
        }

        editor.remove(key);
        editor.apply();
    }


    /**
     * 判断SP是否包含特定key的数据
     * @param key
     * @return
     */
    public boolean contains(String key) {

        if(sharedPreferences == null) {
            init();
        }

        return sharedPreferences.contains(key);
    }
}
